<template>
  <el-dialog :model-value="visible" width="30%"  @close="dialogClose">
    <!--标题-->
    <template #header>
      <div style="font-size:18px; color:#409eff; font-weight:bold;">创建用户</div>
    </template>

    <el-form :model="form" ref="formRef" :rules="formRules"  label-width="100px">
      <el-form-item label="登陆账号：" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="登陆密码：" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="用户名：" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="用户状态：">
        <el-switch v-model="form.status" class="isShow" :active-value="'启用'" :inactive-value="'禁用'" active-color="#13ce66" inactive-color="#73767a" inline-prompt active-text="启用" inactive-text="禁用"/>
      </el-form-item>
      <el-form-item label="部门：" prop="department">
        <el-input v-model="form.department"></el-input>
      </el-form-item>
      <el-form-item label="职位：" prop="position">
        <el-input v-model="form.position"></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserCreate',
  props: {
    visible: Boolean
  },
  data() {
    return {
      form: {
        name: '',
        username: '',
        password: '',
        status: '',
        department: '',
        position: '',
        mobile: '',
        email: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, message: '用户名长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[\u4e00-\u9fa5]+/, message:'用户名不符合规范，请使用中文字符',trigger:'blur'},
        ],
        username: [
          { required: true, message: '请输入登陆账号', trigger: 'blur' },
          { min: 2, message: '登陆账号长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9]+/, message:'登陆账号不符合规范，请使用大小写和数字!',trigger:'blur'},
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 2, message: '用户名长度应不小于6个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9]+/, message:'密码不符合规范，请使用大小写和数字!',trigger:'blur'},
        ],
        department: [
          { min: 2, message: '部门长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9_\-\u4e00-\u9fa5]+/, message:'部门不符合规范，请使用中文字符',trigger:'blur'},
        ],
        position: [
          { min: 2, message: '职位长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9_\-\u4e00-\u9fa5]+/, message:'职位不符合规范，请使用中文字符',trigger:'blur'},
        ],
        mobile:[
          {required:true, message:'手机号码不能为空',trigger:'blur'},
          { min: 11, max: 11, message: '职位长度应不小于2个字符', trigger: 'blur' },
          {pattern:/^[1][35789]\d{9}$/, message:'手机号码必须要符合规范',trigger:'blur'},
        ],
        email:[
          {required:true, message:'邮箱地址不能为空',trigger:'blur'},
          {pattern:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message:'邮箱地址必须要符合规范',trigger:'blur'},
        ],
      }
    }
  },
  methods: {
    submit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http.post('api/user/', this.form).then(res => {
            if (res.data.code == 200) {
              this.$message.success('创建成功')
              this.$parent.GetAllUsers() // 调用父组件方法，更新数据
              this.dialogClose() // 关闭窗口
              this.$refs.formRef.resetFields() // 清空表单数据
            }
          })
        } else {
          this.$message.error('格式错误！')
        }
      })
    },
    // 点击关闭，子组件通知父组件更新属性
    dialogClose() {
      this.$emit('update:visible', false) // 父组件必须使用v-model
    }
  }
}
</script>
